
package com.example.chiro;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Table_arbre {

    public static final String ROW_ID = "_id";
    public static final String PATH_PHOTO = "path_photo";
    public static final String TYPE = "type";
    public static final String HAUTEUR = "hauteur";
    public static final String DIAMETRE = "diametre";
    public static final String STATUT = "statut";
    public static final String ETAT = "etat";
    public static final String PEUPLEMENT = "peuplement";
    public static final String HOUPPIER = "houppier";
    public static final String POTENTIEL = "potentiel";
    public static final String ENVOYE = "envoye";
    
    public static final String DATABASE_TABLE = "arbre";
    private DataBaseHelper DBHelper;
    private SQLiteDatabase DB;
    private final Context context;

    private static class DataBaseHelper extends SQLiteOpenHelper {

        public DataBaseHelper(Context context) {
            super(context, DB_FICHEARBRE_adapter.DATABASE_NAME, null, DB_FICHEARBRE_adapter.DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase arg0) {}
        public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {}

    }

    public Table_arbre(Context c) {
        this.context = c;
    }

    public Table_arbre open() throws SQLException {
        this.DBHelper = new DataBaseHelper(this.context);
        this.DB = this.DBHelper.getWritableDatabase();
        return this;
    }
    
    public void close() {
        this.DBHelper.close();
    }

    public long inserer_unparun(String path_photo, String type, String hauteur,
            String diametre, String statut, String etat, String peuplement, String houppier,
            String potentiel) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(PATH_PHOTO, path_photo);
        initialValues.put(TYPE, type);
        initialValues.put(HAUTEUR, hauteur);
        initialValues.put(DIAMETRE, diametre);
        initialValues.put(STATUT, statut);
        initialValues.put(ETAT, etat);
        initialValues.put(PEUPLEMENT, peuplement);
        initialValues.put(HOUPPIER, houppier);
        initialValues.put(POTENTIEL, potentiel);
        return this.DB.insert(DATABASE_TABLE, null, initialValues);
    }
    
    public long inserer_parSlide(ContentValues content) {
    	return this.DB.insert(DATABASE_TABLE, null, content);
    }

    public boolean delete_donnees_general(long rowId) {
        return this.DB.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0;
    }

	public Cursor getAll() {
		Cursor resultat = null;
		resultat = this.DB.query(DATABASE_TABLE, new String[]{PATH_PHOTO,TYPE,HAUTEUR,DIAMETRE,STATUT,ETAT,PEUPLEMENT,HOUPPIER,POTENTIEL,ENVOYE}, ENVOYE+"=\"false\"", null	, null, null, null, null);
		return resultat;
	}
	
	public Cursor getByID(int id){
		Cursor resultat = null;
		resultat = this.DB.query(DATABASE_TABLE, new String[]{PATH_PHOTO,TYPE,HAUTEUR,DIAMETRE,STATUT,ETAT,PEUPLEMENT,HOUPPIER,POTENTIEL,ENVOYE}, ROW_ID+"=\""+String.valueOf(id)+"\"",null,null,null,null );
		return resultat;
	}
	
	
	public boolean update_envoi(int rowId, String etat) {
	    ContentValues args = new ContentValues();
	    args.put(ENVOYE, etat);
	    return this.DB.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) > 0;
	}
	
	public int nombre_non_envoye() {
		int c = DB.query(DATABASE_TABLE, new String[] { "_id" }, ENVOYE + "=" + "false", null,
				null, null, null).getCount();
		return c;
	}
}
